package com.team.cloudsharing.controller.management;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.team.cloudsharing.dto.Document;
import com.team.cloudsharing.dto.File;
import com.team.cloudsharing.dto.Message;
import com.team.cloudsharing.dto.SharingInfo;
import com.team.cloudsharing.service.management.ManageService;

@RestController
@RequestMapping("/manage")
public class ManageControllerImpl implements ManageController{
	
	final static Logger logger = Logger.getLogger(ManageControllerImpl.class);

	@Autowired
	ManageService manageService;
	
	@Override
	@RequestMapping(value="/getListDocument", method=RequestMethod.GET)
	public ResponseEntity<List<Document>> getListDocument(@RequestParam("userEmail") String userEmail, @RequestParam("path") String path, 
			@RequestParam("trashStatus") String trashStatus) {
		//Example request: http://ip:port/getListDocument?userEmail=vantien.sn92@gmail.com&path=&trashStatus=none
		//trashStatus: none == both in Trash and out Trash, true == in Trash, false == out Trash
//		logger.info("GET /manage/getListDocument - userEmail: " + userEmail + " - path: " + path + " - trashStatus: " + trashStatus);
		List<Document> listDocument = manageService.getListDocument(userEmail, path, trashStatus);
		
		return new ResponseEntity<List<Document>>(listDocument, HttpStatus.OK);
	}
	
	@Override
	@RequestMapping(value="/searchDocument", method=RequestMethod.GET)
	public ResponseEntity<List<Document>> searchDocument(@RequestParam("userEmail") String userEmail, @RequestParam("searchQuery") String searchQuery) {
		logger.info("GET /manage/searchDocument - userEmail: " + userEmail + " - searchQuery: " + searchQuery);
		List<Document> listDocument = manageService.searchDocument(userEmail, searchQuery);
		return new ResponseEntity<List<Document>>(listDocument, HttpStatus.OK);
	}


	@Override
	@RequestMapping(value="/getListImages", method=RequestMethod.GET)
	public ResponseEntity<List<File>> getListImages(@RequestParam("userEmail") String userEmail,@RequestParam("offset") long offset) {
		logger.info("GET /manage/getListImages - userEmail: " + userEmail + " - offset: " + offset);
		List<File> listImages = manageService.getListImages(userEmail,offset);
		return new ResponseEntity<List<File>>(listImages, HttpStatus.OK);
	}

	@Override
	@RequestMapping(value="/getListDocumentInTrash", method=RequestMethod.GET)
	public ResponseEntity<List<Document>> getListDocumentInTrash(@RequestParam("userEmail") String userEmail, @RequestParam("trashStatus") String trashStatus) {
		logger.info("GET /manage/getListDocumentInTrash - userEmail: " + userEmail + " - trashStatus: " + trashStatus);
		List<Document> listDocument = manageService.getListDocumentInTrash(userEmail, trashStatus);
		return new ResponseEntity<List<Document>>(listDocument, HttpStatus.OK);
	}

	
	@Override
	@RequestMapping(value="/getListDocumentInTrashForPC", method=RequestMethod.GET)
	public ResponseEntity<List<Document>> getListDocumentInTrashForPC(@RequestParam("userEmail") String userEmail, @RequestParam("trashStatus") String trashStatus) {
		logger.info("GET /manage/getListDocumentInTrash - userEmail: " + userEmail + " - trashStatus: " + trashStatus);
		List<Document> listDocument = manageService.getListDocumentInTrashForPC(userEmail, trashStatus);
		return new ResponseEntity<List<Document>>(listDocument, HttpStatus.OK);
	}
	
	@Override
	@RequestMapping(value="/getDocumentId", method=RequestMethod.GET)
	public ResponseEntity<Long> getDocumentId(@RequestParam("userEmail") String userEmail,
			@RequestParam("folderPath")String folderPath,@RequestParam("folderName") String folderName,@RequestParam("fileName") String fileName) {
		logger.info("GET /manage/getDocumentId - userEmail: " + userEmail + " - folderPath: " + folderPath  + " - folderName: " + folderName + " - fileName: " + fileName);
		long documentId = manageService.getDocumentId(userEmail, folderPath, folderName, fileName);
		return new ResponseEntity<Long>(documentId, HttpStatus.OK);
	}

	@Override
	@RequestMapping(value="/getListFile", method=RequestMethod.GET)
	public ResponseEntity<List<Document>> getListFile(@RequestParam("userEmail") String userEmail, @RequestParam("path") String path, 
			@RequestParam("trashStatus") String trashStatus) {
		//trashStatus: none == both in Trash and out Trash, true == in Trash, false == out Trash
		logger.info("GET /manage/getListFile - userEmail: " + userEmail + " - path: " + path + " - trashStatus: " + trashStatus);
		List<Document> listDocument = manageService.getListFile(userEmail, path, trashStatus);
		
		return new ResponseEntity<List<Document>>(listDocument, HttpStatus.OK);
	}

	@Override
	@RequestMapping(value="/checkStatus", method=RequestMethod.GET)
	public ResponseEntity<Message> checkStatus(@RequestParam("macAddress") String macAddress) {
		String status = manageService.checkStatus(macAddress);
		return new ResponseEntity<Message>(new Message(status), HttpStatus.OK);
	}

	@Override
	@RequestMapping(value="/setStatus", method=RequestMethod.GET)
	public ResponseEntity<Message> setStatus(@RequestParam("macAddress") String macAddress) {
		// TODO Auto-generated method stub
		manageService.setStatus(macAddress);
		return new ResponseEntity<Message>(new Message("OK"), HttpStatus.OK);
	}

	@Override
	@RequestMapping(value="/getSharingInfomation", method=RequestMethod.GET)
	public ResponseEntity<List<SharingInfo>> getSharingInfomation(@RequestParam("userEmail") String userEmail) {
		List<SharingInfo> listShareInformation = manageService.getSharingInfomation(userEmail);
		return new ResponseEntity<List<SharingInfo>>(listShareInformation, HttpStatus.OK);
	}
}
